/**
 * Copyright (c) 2018 苍穹跃龙
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.l.web.master.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.l.core.util.WebUtils;
import com.l.web.master.system.entity.SysMenu;
import com.l.web.master.system.entity.SysUser;
import com.l.web.master.system.mapper.SysMenuMapper;
import com.l.web.master.system.service.ISysMenuService;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author 严秋旺
 * @since 2018-09-27
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Override
    public boolean insert(SysMenu entity) {
        if (entity.getPid() == null || entity.getPid() == ROOT_ID) {
            entity.setPid(ROOT_ID);
            entity.setPath(ROOT_PATH);
            entity.setDeep(1);
        } else {
            SysMenu parent = baseMapper.selectById(entity.getPid());
            entity.setPath(parent.getPath() + "," + parent.getId());
            entity.setDeep(parent.getDeep() + 1);
        }

        return retBool(this.baseMapper.insert(entity));
    }

    @Override
    public boolean updateById(SysMenu entity) {
        if (entity.getPid() == null || entity.getPid() == ROOT_ID) {
            entity.setPid(ROOT_ID);
            entity.setPath(ROOT_PATH);
            entity.setDeep(1);
        } else {
            SysMenu parent = baseMapper.selectById(entity.getPid());
            entity.setPath(parent.getPath() + "," + parent.getId());
            entity.setDeep(parent.getDeep() + 1);
        }

        return retBool(this.baseMapper.updateById(entity));
    }

    @Override
    public List<SysMenu> findUserMenuTree() {
        SysUser user = WebUtils.getLogin();
        if(user == null) {
            return new ArrayList<>();
        }
        
        List<SysMenu> list = baseMapper.findUserMenuTree(user);
        Map<Integer, SysMenu> map = new HashMap<>();
        List<SysMenu> root = new ArrayList<>();

        list.forEach(menu -> {
            SysMenu parent = map.get(menu.getPid());

            if (parent == null) {
                root.add(menu);
            } else {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }

                parent.getChildren().add(menu);
            }

            map.put(menu.getId(), menu);
        });

        return root;
    }

    @Override
    public List<SysMenu> findAllMenu() {
        EntityWrapper<SysMenu> wrapper = new EntityWrapper<>();
        wrapper.orderBy("deep", true).orderBy("sort", true);
        return baseMapper.selectList(wrapper);
    }

}
